/*
 * Copyright 2016 SoChishun.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package util;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * XML与Bean序列化通用类(XML)
 * 
 * @author SoChishun
 * @version 1.0 2016-5-25
 * @see http://bxl766.blog.163.com/blog/static/610995052011111254316813/
 */
public class JAXBUtil {

    /**
     * Bean转为XML
     *
     * <p>需添加注解@XmlRootElement</p>
     * <p>serialize(conninfo, "./Data/connection.xml");</p>
     * @param obj
     * @param file
     * @throws JAXBException
     */
    public static void serialize(Object obj, File file) throws JAXBException {
        JAXBContext ctx = JAXBContext.newInstance(obj.getClass());
        //JAXBContext可以创建Marshallers（将Java对象转换成XML）
        Marshaller mar = ctx.createMarshaller();
        mar.marshal(obj, file);
    }

    /**
     * Bean转为XML
     *
     * <p>需添加注解@XmlRootElement</p>
     * <p>serialize(conninfo, "./Data/connection.xml");</p>
     * @param obj
     * @param filename
     * @throws JAXBException 
     * @since 1.0 2016-5-25 SoChishun Added.
     */
    public static void serialize(Object obj, String filename) throws JAXBException {
        JAXBContext ctx = JAXBContext.newInstance(obj.getClass());
        //JAXBContext可以创建Marshallers（将Java对象转换成XML）
        Marshaller mar = ctx.createMarshaller();
        mar.marshal(obj, new File(filename));
    }

    /**
     * XML转为Bean
     *
     * <p>需添加注解@XmlRootElement</p>
     * <p>conninfo=deserialize(ConnectionInfo.class, con_path);</p>
     * @param cls
     * @param filename
     * @return
     * @throws JAXBException
     * @since 1.0 2016-5-25 SoChishun Added.
     */
    public static <T> T deserialize(Class<T> cls, String filename) throws JAXBException {
        JAXBContext ctx = JAXBContext.newInstance(cls);
        //JAXBContext可以创建 Unmarshallers（将XML解析为Java对象）
        Unmarshaller us = ctx.createUnmarshaller();
        return (T) us.unmarshal(new File(filename));
    }

    /**
     * XML转为Bean
     *
     * <p>需添加注解@XmlRootElement</p>
     * <p>conninfo=deserialize(ConnectionInfo.class, new File(con_path));</p>
     * @param cls
     * @param file
     * @return
     * @throws JAXBException
     */
    public static Object deserialize(Class cls, File file) throws JAXBException {
        JAXBContext ctx = JAXBContext.newInstance(cls);
        //JAXBContext可以创建 Unmarshallers（将XML解析为Java对象）
        Unmarshaller us = ctx.createUnmarshaller();
        return us.unmarshal(file);
    }
}
